package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.dto.EmpQueryParam;
import com.itheima.pojo.LoginInfo;
import com.itheima.pojo.PageResult;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> list = empMapper.list(empQueryParam);
        Page<Emp> empList = (Page<Emp>) list;
        return new PageResult(empList.getTotal(),empList.getResult());
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);

        Integer id = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(id));
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public List<Emp> finAll() {empMapper.findAll();
        return empMapper.findAll();
    }

    @Override
    public void delete(List<Integer> ids) {
        //根据id删除员工信息
        empMapper.delete(ids);
        //根据员工id删除员工经历
        empExprMapper.deleteById(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getInfo(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

        List<Integer> list = Arrays.asList(emp.getId());
        empExprMapper.deleteById(list);

        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(empId);
            }
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public Integer getDeptId(Integer id) {
        Integer deptId = empMapper.getDeptId(id);
        return deptId;
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp emp1 = empMapper.login(emp);
        if (emp1 != null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",emp1.getId());
            map.put("username",emp1.getUsername());
            return new LoginInfo(emp1.getId(),emp1.getUsername(),emp1.getName(), JwtUtils.getJwt(map));
        }
        return null;
    }
}
